package org.jeecg.modules.activiti.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.jeecg.modules.activiti.entity.ActEaTask;

import java.util.List;
import java.util.Map;

/**
 * @Description: ea_task
 * @Author: qixin-boot
 * @Date:   2021-09-18
 * @Version: V1.0
 */
public interface ActEaTaskMapper extends BaseMapper<ActEaTask> {

    //删除运行时任务节点表-待办任务--待办任务id
    @Delete("delete from act_ru_task where id_ = #{taskId}")
    int deleteRuTask(@Param("taskId")String taskId);

    //删除历史流程任务表的任务-待办任务id
    @Delete("delete from act_hi_taskinst where id_ = #{taskId}")
    int deleteHiTask(@Param("taskId")String taskId);

    //获取已办任务里的最新的任务id(当前流程实例,同时end_time_不为空,task_id不为空)
    @Select("select * from act_hi_taskinst where end_time_ is not null and proc_inst_id_ = #{procinstId} order by start_time_ desc limit 1 ")
    Map<String,Object> selectLastHiTask(String procinstId);

    //获取已办任务里的最新的任务id(再前一个已办任务id)(当前流程实例,同时end_time_不为空,task_id不为空)
    @Select("select * from act_hi_taskinst where end_time_ is not null and proc_inst_id_ = #{procinstId} and id_ != #{lastTaskId} order by start_time_ desc limit 1 ")
    Map<String,Object> selectLast2HiTask(String procinstId,String lastTaskId);

    //获取已办任务里特征表中的最新的数据:lastTaskId,procInstId
    @Select("select * from act_hi_identitylink where task_id_ = #{lastTaskId} and proc_inst_id_ is null ")
    List<Map<String, Object>> selectLastHiIdentity(String lastTaskId);

    //更新历史任务表已办任务(act_hi_taskinst)字段:id_(已办任务id)::结束时间清空,持续时间清空,删除理由清空
    @Update("update act_hi_taskinst set end_time_ = null,duration_ = null,delete_reason_ = null where id_ = #{lastTaskId} ")
    void updateActHiTask(String lastTaskId);

    //删除历史流程人员表-待办任务(act_hi_identitylink):task_id_(当前任务id)::节点实例id
    @Delete("delete from act_hi_identitylink where task_id_ = #{taskId} ")
    void deleteActHiIdentityByTaskId(String taskId);

    //删除历史流程人员表-已办任务(act_hi_identitylink):task_id_(已办任务id)::流程实例id
    @Delete("delete from act_hi_identitylink where task_id_ = #{lastTaskId} and proc_inst_id_ = #{procInstId}  ")
    void deleteActHiIdentity(String lastTaskId,String procInstId);

    //删除历史意见表-意见(act_hi_comment):task_id_(已办任务id)
    @Delete("delete from act_hi_comment where task_id_ = #{lastTaskId} ")
    void deleteActHiComment(String lastTaskId);

    //删除运行时流程人员表-节点参与者(act_ru_identitylink):task_id_(待办任务id)
    @Delete("delete from act_ru_identitylink where task_id_ = #{taskId} ")
    void deleteActRuIdentity(String taskId);

    //将查询出来的已办任务(act_hi_taskinst)数据,插入到待办任务表(act_ru_task)
    @Insert("insert into act_ru_task (ID_,REV_,EXECUTION_ID_,PROC_INST_ID_,PROC_DEF_ID_,NAME_,TASK_DEF_KEY_,OWNER_,ASSIGNEE_,PRIORITY_,CREATE_TIME_,SUSPENSION_STATE_) " +
            "values (#{ID_},'1',#{EXECUTION_ID_},#{PROC_INST_ID_},#{PROC_DEF_ID_},#{NAME_},#{TASK_DEF_KEY_},#{OWNER_},#{ASSIGNEE_},#{PRIORITY_},#{START_TIME_},'1') ")
    void insertActRuTask(Map<String, Object> actHiTaskMap);

    //删除历史节点表(act_hi_actinst)中,(已办任务id,并且时间大于任务开始时间的所有记录)
    @Delete("delete from act_hi_actinst where start_time_ > #{startTime}  ")
    void deleteActHiActinst(String startTime);

    //查询任务的开始时间
    @Select("select START_TIME_,DELETE_REASON_,PROC_INST_ID_ from act_hi_taskinst where id_ = #{lastTaskId}")
    Map<String,Object> selectActHiActinst(String lastTaskId);

    //更新历史节点表(act_hi_actinst)中,字段:task_id_(已办任务id),将结束日期清空,持续时间清空
    @Update("update act_hi_actinst set end_time_ = null,duration_ = null where task_id_ = #{lastTaskId} ")
    void updateActHiActinst(String lastTaskId);

    //更新运行时流程执行实例表(act_ru_execution)字段:act_id_为上一节点任务的task_def_key_值
    @Update("update act_ru_execution set act_id_ = #{taskDefKey} where proc_inst_id_ = #{procinstId} ")
    void updateActRuExecution(String procinstId,String taskDefKey);

    //将上一个节点已办的任务特征表(act_hi_identitylink),插入到待办的特征表(act_ru_identitylink)
    @Insert("insert into act_ru_identitylink (ID_,REV_,TYPE_,USER_ID_,TASK_ID_) values (#{ID_},'1','candidate',#{USER_ID_},#{TASK_ID_}) ")
    void insertActRuIdentity(Map<String, Object> actHiIdentityMap);

    //查询已办任务总条数(act_hi_taskinst),如果条数是1,就说明是初始状态
    @Select("select count(*) from act_hi_taskinst where proc_inst_id_ = #{procinstId} ")
    int selectActHiActinstCount(String procinstId);

    //查询待办任务
    @Select("select * from act_ru_task where id_ = #{taskId} ")
    Map<String,Object> selectActRuTask(String taskId);

    //查询待办任务,(查询大于当前已办任务开始时间的所有已办任务记录)
    @Select("select * from act_hi_taskinst where start_time_ > #{startTime} and proc_inst_id_ = #{procInstId} ")
    List<Map<String,Object>> selectActHiTaskFromTime(String startTime,String procInstId);

    //查询待办任务,(查询小于当前已办任务开始时间的所有已办任务记录)
    @Select("select * from act_hi_taskinst where start_time_ < #{startTime} and proc_inst_id_ = #{procInstId} order by start_time_ desc limit 1 ")
    List<Map<String,Object>> selectActHiTaskFromTime2(String startTime,String procInstId);
}
